home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / ChangeDialog.C < prev    next >
C/C++ Source or Header  |  1992-08-10  |  4KB  |  166 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "ChangeDialog.h"
  6.  
  7. #include "FindChange_e.h"
  8. #include "ET++.h"
  9. #include "RegularExp.h"
  10. #include "TextView.h"
  11. #include "TextCmd.h"
  12. #include "BorderItems.h"
  13. #include "OneOfCluster.h"
  14. #include "ManyOfCluster.h"
  15. #include "Fields.h"
  16. #include "Buttons.h"
  17. #include "Form.h"
  18. #include "Progress.h"
  19. #include "CheapText.h"
  20.  
  21. //---- ChangeDialog ------------------------------------------------------------
  22.  
  23. NewMetaImpl(ChangeDialog,FindDialog, (TP(scopecl)));
  24.  
  25. ChangeDialog::ChangeDialog(char *title) : FindDialog(title)
  26. {
  27. }
  28.  
  29. ChangeDialog::~ChangeDialog()
  30. {
  31. }
  32.  
  33. VObject *ChangeDialog::DoMakeContent()
  34. {
  35.     return
  36.     new Matte(
  37.         new VBox(10, eVObjHExpand,
  38.         new Form(
  39.             "Find:",   ei1= new TextField(cIdFind, 20),
  40.             "Change:", ei2= new TextField(cIdChange, 20),
  41.             0
  42.         ),
  43.         new HBox(10, (VObjAlign)(eVObjVTop|eVObjHGapExpand),
  44.             new BorderItem("Direction", 
  45.             modecl= new OneOfCluster(cIdFindMode,
  46.                 "Forward",
  47.                 "Backward",
  48.                 0
  49.             )
  50.             ),
  51.             new BorderItem("Options",
  52.             optionscl= new ManyOfCluster(cIdFindOpt,
  53.                 "Ignore Case",
  54.                 "Match Whole Word",
  55.                 0
  56.             )
  57.             ),
  58.             new BorderItem("Change Scope",
  59.             scopecl= new OneOfCluster(cIdChangeAllScope,
  60.                 "All of Document",
  61.                 "Selection Only",
  62.                 0
  63.             )
  64.             ),
  65.             0 
  66.         ),
  67.         new HBox(10, (VObjAlign)(eVObjVBase|eVObjHEqual|eVObjHGapExpand),
  68.             find= new ActionButton(cIdDoFind,           "Find Next", TRUE), 
  69.             change= new ActionButton(cIdDoChange,       "Change, Then Find"), 
  70.             changeAll= new ActionButton(cIdDoChangeAll, "Change All"),
  71.             new ActionButton(cHELP, "Help"),
  72.             0
  73.         ),
  74.         0
  75.         )
  76.     );
  77. }
  78.  
  79. void ChangeDialog::DoSetup()
  80. {
  81.     FindDialog::DoSetup();
  82.     bool b= ei1->GetTextSize() > 0;
  83.     change->Enable(b);
  84.     changeAll->Enable(b);
  85. }
  86.  
  87. void ChangeDialog::Control(int id, int p, void *v)
  88. {
  89. char b[100], b2[200];
  90.     switch (id) {
  91.     case cIdDoChange:
  92.     ei1->GetString(b, 100);
  93.     ei2->GetString(b2, 200);
  94.     DoChange(b, b2);
  95.     return;
  96.  
  97.     case cIdDoChangeAll:
  98.     DoChangeAll(ei1->GetString(), ei2->GetString());
  99.     return;
  100.     }
  101.     FindDialog::Control(id, p, v); 
  102. }
  103.  
  104. void ChangeDialog::DoChange(char *what, char *c)
  105. {
  106.     int from, to, f1, t1;
  107.  
  108.     tvp->GetSelection(&from, &to);
  109.     bool forward= modecl->GetCurrentItem() == cIdForward;
  110.     
  111.     if (forward)
  112.     tvp->SetSelection(from, from);
  113.     else
  114.     tvp->SetSelection(to, to);
  115.     if (DoFind(what, forward)) {
  116.     tvp->GetSelection(&f1, &t1);
  117.     if (f1 == from && t1 == to) {
  118.         tvp->GetSelection(&from, &to);
  119.         tvp->PerformCommand(tvp->InsertString((byte*)c));
  120.         if (!forward)
  121.         tvp->SetSelection(from, from);
  122.         DoFind(what, forward);
  123.     }
  124.     }
  125.     tvp->RevealSelection();
  126. }
  127.  
  128. void ChangeDialog::DoChangeAll(char *find, char *change)
  129. {
  130.     int from, to;
  131.     tvp->GetSelection(&from, &to);
  132.     if (scopecl->GetCurrentItem() == cIdChangeAll) {
  133.     from= 0;
  134.     to= tvp->GetText()->End();
  135.     }
  136.     int nChanges= ChangeAll(from, to, find, change);
  137.     if (nChanges >= 0)
  138.     ShowAlert(eAlertNote, "%d occurrences changed", nChanges);
  139. }
  140.  
  141. int ChangeDialog::ChangeAll(int from, int to, char *find, char *change)
  142. {
  143.     int start= 0, n= 0, nChanges= 0;
  144.     Text *tmp= tvp->GetText()->Save(from, to);
  145.     if (!Compile(find))
  146.     return -1;
  147.     Text *changeText= tmp->MakeScratchText((byte*)change, strlen(change));
  148.     gProgress->Start("Searching", 100);
  149.     for (int i= 0;; ) {
  150.     start= tmp->Search(rex, &n, start);
  151.     if (start == -1)
  152.         break;
  153.     tmp->ReplaceRange(start, start+n, changeText, 0, changeText->End());
  154.     start+= n;
  155.     nChanges++;
  156.     gProgress->Tick(i++);
  157.     }
  158.     gProgress->Stop();
  159.     
  160.     if (nChanges) {
  161.     tvp->SetSelection(from, to, FALSE);
  162.     tvp->PerformCommand(new PasteCommand(tvp, tmp, cPASTE, "Change All"));
  163.     }
  164.     return nChanges;
  165. }     
  166.